package com.ihr360.expr;

import com.ihr360.expr.core.AbstractMathematicalOperator;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprType;

import java.math.BigDecimal;

/**
 * 指数表达式
 */
public class ExprPower extends AbstractMathematicalOperator
{
    public ExprPower(Expr lhs, Expr rhs) {
        super(ExprType.Power, lhs, rhs);
    }

    @Override
    protected Expr evaluate(double lhs, double rhs) throws ExprException {
        return new ExprDouble(Math.pow(lhs, rhs));
    }

    @Override
    protected Expr evaluate(BigDecimal lhs, BigDecimal rhs) throws ExprException {
        if (rhs==null){
            rhs = BigDecimal.ZERO;
        }
        return new ExprBigDecimal(lhs.pow(rhs.intValue()));
    }

    @Override
    public String toString() {
        return lhs + "^" + rhs;
    }
}